package com.echo.adapter;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.echo.activity.R;
import com.echo.model.Sport;
import com.echo.util.TimeUtil;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by EchoGe on 2016/6/13.
 */
public class HistoryDayRecordAdapter extends RecyclerView.Adapter<HistoryDayRecordAdapter.ViewHolder>{

    private Context mContext;
    private List<Sport> mSports=new ArrayList<>() ;

    public HistoryDayRecordAdapter(){}

    public HistoryDayRecordAdapter(Context context){
        this.mContext=context;
    }

    public HistoryDayRecordAdapter(Context context,List<Sport> sports){
        this.mContext=context;
        this.mSports=sports;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        public TextView distance,time,calories,date;
        public ImageView path,show_hide;
        public RelativeLayout layout;

        public ViewHolder(View itemView) {
            super(itemView);
            distance= (TextView) itemView.findViewById(R.id.day_record_data_distance);
            time= (TextView) itemView.findViewById(R.id.day_record_data_time);
            calories= (TextView) itemView.findViewById(R.id.day_record_data_calories);
            path= (ImageView) itemView.findViewById(R.id.day_record_data_path);
            layout= (RelativeLayout) itemView.findViewById(R.id.day_record_layout);
            date= (TextView) itemView.findViewById(R.id.history_day_record_date);
            show_hide= (ImageView) itemView.findViewById(R.id.history_day_record_show_hide);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_history_day_record,parent,false);
        ViewHolder viewHolder=new ViewHolder(view);
        view.setTag(viewHolder);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ViewHolder viewHolder1= (ViewHolder) v.getTag();
                if(viewHolder1.layout.getVisibility()==View.VISIBLE) {
                    viewHolder1.layout.setVisibility(View.GONE);
                    viewHolder1.show_hide.setImageBitmap(BitmapFactory.decodeResource(mContext.getResources(),R.drawable.ic_keyboard_arrow_down_black_36dp));
                }
                else {
                    viewHolder1.layout.setVisibility(View.VISIBLE);
                    viewHolder1.show_hide.setImageBitmap(BitmapFactory.decodeResource(mContext.getResources(),R.drawable.ic_keyboard_arrow_up_black_36dp));
                }
            }
        });
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.date.setText(mSports.get(position).getStart().getTime().getHours()+":"+mSports.get(position).getStart().getTime().getMinutes());
        holder.distance.setText(String.valueOf(mSports.get(position).getDistance()));
        holder.time.setText(String.valueOf(TimeUtil.secondsToStr(mSports.get(position).getTime())));
        holder.calories.setText(String.valueOf(mSports.get(position).getCalories()));
        byte[] image=mSports.get(position).getSnapshot();
        if(image!=null&&image.length>0){
            holder.path.setImageBitmap(BitmapFactory.decodeByteArray(mSports.get(position).getSnapshot(),0,mSports.get(position).getSnapshot().length));
        }else{
            String url=mSports.get(position).getSnapshotUrl();
            if(url!=null&&url.length()>0){
                ImageLoader imageLoader=ImageLoader.getInstance();
                imageLoader.init(ImageLoaderConfiguration.createDefault(mContext));
                imageLoader.displayImage(url, holder.path);
            }else{
                holder.path.setImageBitmap(BitmapFactory.decodeResource(mContext.getResources(),R.mipmap.fv));
            }
        }
    }

    @Override
    public int getItemCount() {
        return mSports.size();
    }

}
